package com.czxy.controller;


import com.czxy.domain.User;
import com.czxy.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {


    @Resource
    private UserService userService;




    @GetMapping
    public ResponseEntity findAll(){

        try{

            List<User> list = userService.findAll();

            return  ResponseEntity.ok(list);


        }catch (Exception e){

            e.printStackTrace();
            return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }



    @PostMapping
    public  ResponseEntity add(User user){

        try{

            userService.add(user);

            return  new ResponseEntity(HttpStatus.NO_CONTENT);
        }catch (Exception e){
            e.printStackTrace();
            return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }











    }




    @GetMapping("/{uid}")
    public  ResponseEntity findById(@PathVariable("uid") Integer uid){

        try{

            User user = userService.findById(uid);

            return  ResponseEntity.ok(user);


        }catch (Exception e){

            e.printStackTrace();
            return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }


    @PutMapping
    public  ResponseEntity update(User user){

        try{

            userService.update(user);

            return  new ResponseEntity(HttpStatus.NO_CONTENT);


        }catch (Exception e){

            e.printStackTrace();
            return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }







    @DeleteMapping("/{uid}")
    public  ResponseEntity delete( @PathVariable("uid") Integer uid){

        try{

            userService.deleteById(uid);

            return  new ResponseEntity(HttpStatus.NO_CONTENT);


        }catch (Exception e){

            e.printStackTrace();
            return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }

    @PostMapping("/upload")
    public  ResponseEntity upload(User user , MultipartFile mFile){


        try{


            //打印用户名和密码
            System.out.println(user);
            //文件存储到指定位置
            String originalFilename = mFile.getOriginalFilename();

            File f = new File("D:\\ala\\" + originalFilename);
            mFile.transferTo(f);
            System.out.println("文件存储完毕");

            return  new ResponseEntity(HttpStatus.NO_CONTENT);

        }catch (Exception e){

            e.printStackTrace();
            return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }

    @PostMapping("/login")
    public  ResponseEntity login(User user , HttpSession session){
        try{


            User loginU = userService.login(user);
            if (loginU!=null) {

                session.setAttribute("loginU", loginU);

                return new ResponseEntity(HttpStatus.NO_CONTENT);
            }else{

                return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);

            }


        }catch (Exception e){

            e.printStackTrace();
            return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }









































}
